/**
 * 快速获取当前时间
 * @param {String} str 例如：y-m-r h:i:s week
 * @returns String
 */

function format(str){
    var d = new Date()
    var arr = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六']
    str = str.replace('y',d.getFullYear())
    str = str.replace('m',d.getMonth()+1)
    str = str.replace('r',d.getDate())
    str = str.replace('h',String(d.getHours()).padStart(2,0))
    str = str.replace('i',String(d.getMinutes()).padStart(2,0))
    str = str.replace('s',String(d.getSeconds()).padStart(2,0))
    str = str.replace('week',arr[d.getDay()])
    return str
}



/**
 * 日期格式化
 * @param {String} date 年-月-日 时:分:秒
 * @returns String
 */

function dateFormat(time){
    var nowDate = new Date()
    var oldDate = new Date(time)
    var diff = ( nowDate.getTime() - oldDate.getTime() ) / 1000
    var result
    if (diff < 60){
        result = '刚刚'
    } 
    else if (diff/60 >= 1 && diff/60 < 60){
        result = parseInt(diff/60) + '分钟前'
    } 
    else if (diff/60/60 >= 1 && diff/60/60 < 24){
        result = parseInt(diff/60/60) + '小时前'
    } 
    else if (diff/60/60/24 >= 1 && diff/60/60/24 < 30){
        result = parseInt(diff/60/60/24) + '天前'
    } 
    else if (diff/60/60/24/30 >= 1 && diff/60/60/24/30 < 12){
        result = parseInt(diff/60/60/24/30) + '个月前'
    } 
    else {
        result = parseInt(diff/60/60/24/30/12) + '年前'
    }
    return result
}



/**
 * 获取随机十六进制颜色
 * @param {String} color
 * @returns String
 */
function randomColor(color){
        var arr = [0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F']
        var max = arr.length-1
        var min = 0
        var one = arr[Math.floor( Math.random() * (max-min+1)+min)]
        var two = arr[Math.floor( Math.random() * (max-min+1)+min)]
        var three = arr[Math.floor( Math.random() * (max-min+1)+min)]
        var four = arr[Math.floor( Math.random() * (max-min+1)+min)]
        var five = arr[Math.floor( Math.random() * (max-min+1)+min)]
        var six = arr[Math.floor( Math.random() * (max-min+1)+min)]
        color = '#' + one + two + three + four + five + six
    return color
}



/**
 * 判断用户浏览器
 * @param {String} getBrowserEngine  
 * @returns String
 */
function getBrowserEngine(){
    if (navigator.userAgent.indexOf('Chrome') !== -1){
        return 'Chrome'
} 
    else if (navigator.userAgent.indexOf('Firefox') !== -1){
        return 'Firefox'
} 
    else if (navigator.userAgent.indexOf('Trident') !== -1){
        return 'IE'
} 
    else if (navigator.userAgent.indexOf('Sarfari') !== -1){
        return 'Sarfari'
}
}



/**
* 判断是PC端还是移动端
* @param {String} isPcOrMobile 
* @return String // 移动端/PC端
*/
function isPcOrMobile() {
    var browser = {
      versions: function () {
        var u = navigator.userAgent;
        return {
          mobile: u.indexOf('Mobile') > -1, //是否为移动终端
          ios: u.indexOf('Mac OS X') > -1, //ios
          android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android
          iPhone: u.indexOf('iPhone') > -1, //iPhone
          iPad: u.indexOf('iPad') > -1, //iPad
          webApp: u.indexOf('Safari') > -1 //Safari
        }
      }()
    }
    if (browser.versions.mobile || browser.versions.ios || browser.versions.android || browser.versions.iPhone || browser.versions.iPad) {
      return '移动端'
    } else {
      return 'PC端'
    }
}


/**
* 判断是否是微信浏览器
* @param {String} getWechatBrowser  
* @returns String
*/
function getWechatBrowser(){
    if (navigator.userAgent.indexOf('micromessenger') !== -1) {
        return '微信浏览器'
    }
      else {
        return '不是微信浏览器'
    }
}


/**
* 获取非行内样式（兼容ie低版本浏览器）
* @param {Object} tagObj 
* @param {String} cssAttr
* @return String
*/
function getStyle(tagObj,cssAttr){
    if(window.getComputedStyle){
        return getComputedStyle(tagObj)[cssAttr]
    }else{
        return tagObj.currentStyle[cssAttr]
    }
}


/**
 * 获取地址栏参数
 * @param {String} key 键
 * @returns 
 */
function getUrlParams(key){
    // 1、字符串转数组，按照【?】划分，获取后面的参数【注意解码地址】
    let urlStr = decodeURI(location.href)
    let paramsStr = urlStr.split('?')[1]
    
    // 判断地址栏是否有参数，如果没有就返回undefined
    if(!paramsStr) return

    // 2、将上面获取的参数转成数组，按照【&】划分，遍历数组      =>      举例：['a=1',...,'b=2']
    let newArr = paramsStr.split('&')
    for(let i = 0 ; i < newArr.length ; i++){
        // 3、将上面数组的值item转成数组，按照【=】划分     =>     举例：temp=['a',1] ... temp=['b',2]
        let item = newArr[i]
        let temp = item.split('=')
        let val = temp[1]

        // 4、将上面数组的第一个值当做key,第二个值当做val,判断键key是不是等于第一个值，如果成立就返回第二个值val
        if(key === temp[0]){
            return val
        }
    }
}
